/**
 *
 * <pre>
 * 实例
 * new WorkflowApp({
 *    target : "#drawing",
 *    language : "",//国际化配置
 *    scene : "",//状态
 *    currentNodeId : ""
 * })
 * </pre>
 *
 * --------------------------------
 *
 * API:
 *
 * loadWorkflow(params);
 *
 *
 */
const WorkflowApp = (function () {

    'use strict';

    const logger = WorkflowUtil.newLogger("WorkflowApp");

    const WorkflowApp = function (config) {

        this.setting = null;//设置信息

        //MVC模型
        this.controller = null;


        //默认配置
        const defaultConfig = {
            target: "",
            language: "zh_CN",
            scene : WorkflowConstant.scene.RUNNING_CHECK_GENERAL,
            currentNodeId : ""
        };

        config = WorkflowUtil.merge(config, defaultConfig);

        //设置整个系统是否是调试模式
        WorkflowApp.prototype.language = config.language;

        this._init(config);
    };


    /**
     *
     * 初始化工作流
     *
     * @param setting
     */
    WorkflowApp.prototype._init = function (setting) {

        this.setting = setting;

        // 初始化Controller
        this.controller = new WorkflowApp.Controller({
            container: setting.target
        });
    };

    //API定义
    const apiDefs = {
        "loadWorkflow": "_loadWorkflow",
        "toXMLObject" : "_toXMLObject"
    };


    for (let key in apiDefs) {

        const functionName = apiDefs[key];
        if (key === functionName) {
            logger.error("错误的接口定义， 方法名和实际执行函数相同了.");
            continue;
        }

        WorkflowApp.prototype[key] = (function (functionName) {
            return function () {
                if (!WorkflowApp.prototype[functionName]) {
                    logger.error(functionName + "接口未定义");
                } else {
                    //console.info("接口拦截设置...");
                    // TODO 这里可以拦截所有的API接口
                    return WorkflowApp.prototype[functionName].apply(this, arguments);
                }
            }
        })(functionName);
    }


    /**
     * 加载工作流数据
     * params = ｛
     *   processXml : "",
     *   caseLogXml : "",
     *   caseWorkitemLogXml : ""
     * ｝
     *
     */
    WorkflowApp.prototype._loadWorkflow = function (params) {
        //初始化模型
        this.controller.loadProcess({
            processXml: params.processXml,
            caseLogXml: params.caseLogXml,
            caseWorkitemLogXml: params.caseWorkitemLogXml
        });

        return this;
    };


    /**
     * 获取process的xml数据
     * @private
     */
    WorkflowApp.prototype._toXMLObject = function(){


        const processXMLDoc = WorkflowUtil.getEmptyXMLDoc();

        if(!processXMLDoc){
            return "";
        }

        const psNode = processXMLDoc.createElement("ps");
        processXMLDoc.appendChild(psNode);

        this.controller.parseProcess2XML(psNode, processXMLDoc);

        return WorkflowUtil.xml2String(processXMLDoc);
    };

    return WorkflowApp;
})();
